package com.ruoyi.tb.lmt.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.ExcelUtils;
import com.ruoyi.tb.lmt.domain.bo.ElectronicPendulumdetailDataBo;
import com.ruoyi.tb.lmt.domain.vo.ElectronicPendulumdetailDataVo;
import com.ruoyi.tb.lmt.service.IElectronicPendulumdetailDataService;
import com.ruoyi.tb.lmt.service.impl.ElectronicPendulumdetailDataServiceImpl;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.ElectronicPendulumDataVo;
import com.ruoyi.tb.lmt.domain.bo.ElectronicPendulumDataBo;
import com.ruoyi.tb.lmt.service.IElectronicPendulumDataService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电子摆数据主表
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/pendulumData")
public class ElectronicPendulumDataController extends BaseController {

    private final IElectronicPendulumDataService iElectronicPendulumDataService;

    private final IElectronicPendulumdetailDataService iElectronicPendulumdetailDataService;
    /**
     * 查询电子摆数据主表列表
     */
    //@SaCheckPermission("xha-lmt:pendulumData:list")
    @GetMapping("/list")
    public TableDataInfo<ElectronicPendulumDataVo> list(ElectronicPendulumDataBo bo, PageQuery pageQuery) {
        return iElectronicPendulumDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电子摆数据主表列表
     */
    //@SaCheckPermission("xha-lmt:pendulumData:export")
    @Log(title = "电子摆数据主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ElectronicPendulumDataBo bo, HttpServletResponse response) {
        List<ElectronicPendulumDataVo> list = iElectronicPendulumDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "电子摆数据主表", ElectronicPendulumDataVo.class, response);
    }

    /**
     * 获取电子摆数据主表详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("xha-lmt:pendulumData:query")
    @GetMapping("/{id}")
    public R<ElectronicPendulumDataVo> getInfo(@NotNull(message = "主键不能为空")
                                               @PathVariable Long id) {
        return R.ok(iElectronicPendulumDataService.queryById(id));
    }

    /**
     * 新增电子摆数据主表
     */
    //@SaCheckPermission("xha-lmt:pendulumData:add")
    @Log(title = "电子摆数据主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ElectronicPendulumDataBo bo) {
        return toAjax(iElectronicPendulumDataService.insertByBo(bo));
    }

    /**
     * 修改电子摆数据主表
     */
    //@SaCheckPermission("xha-lmt:pendulumData:edit")
    @Log(title = "电子摆数据主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ElectronicPendulumDataBo bo) {
        return toAjax(iElectronicPendulumDataService.updateByBo(bo));
    }

    /**
     * 删除电子摆数据主表
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("xha-lmt:pendulumData:remove")
    @Log(title = "电子摆数据主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iElectronicPendulumDataService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //查询电子摆详细数据
    @GetMapping("/selectDetailData")
    public List<ElectronicPendulumdetailDataVo> selectDetailData(String lmtId, String testDate, String reportNumber) {
        return iElectronicPendulumDataService.selectPendulumData(lmtId, testDate, reportNumber);
    }


    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        Sheet sheet = ExcelUtils.getSheet(file.getInputStream(), file.getOriginalFilename(), 0);
        ElectronicPendulumDataBo bo = new ElectronicPendulumDataBo();
        Row row1 = sheet.getRow(1);
        bo.setCompany(getCellValue(sheet.getRow(3).getCell(0)));
        bo.setWorkUser(getCellValue(row1.getCell(1)));
        bo.setWorkUser2(getCellValue(row1.getCell(4)));
        bo.setWorkUser3(getCellValue(row1.getCell(7)));
        Row row6 = sheet.getRow(6);
        bo.setProductName(getCellValue(row6.getCell(1)));
        bo.setProductNumber(getCellValue(row6.getCell(4)));
        bo.setLmtId(bo.getProductNumber().substring(0, bo.getProductNumber().indexOf("-")));
        bo.setReportNumber(getCellValue(row6.getCell(7)));
        Row row8 = sheet.getRow(8);
        bo.setProductModel(getCellValue(row8.getCell(1)));
        bo.setProductFeature(getCellValue(row8.getCell(4)));
        bo.setFullRange(getCellValue(row8.getCell(7)));
        Row row10 = sheet.getRow(10);
        bo.setTestItem(getCellValue(row10.getCell(1)));
        bo.setTestAttribute(getCellValue(row10.getCell(4)));
        bo.setResponseTime(getCellValue(row10.getCell(7)));
        Row row13 = sheet.getRow(13);
        bo.setAmbientTemperature(getCellValue(row13.getCell(1)));
        bo.setStandardTemperature(getCellValue(row13.getCell(2)));
        bo.setAmbientHumidity(getCellValue(row13.getCell(4)));
        bo.setStandardHumidity(getCellValue(row13.getCell(5)));
        Row row16 = sheet.getRow(16);
        bo.setMaxLimitError(getCellValue(row16.getCell(1)));
        bo.setMaxLinearity(getCellValue(row16.getCell(4)));
        bo.setMaxGapError(getCellValue(row16.getCell(7)));
        Row row19 = sheet.getRow(19);
        bo.setLimitErrorStandardValue(getCellValue(row19.getCell(1)));
        bo.setLinearityStandardValue(getCellValue(row19.getCell(4)));
        bo.setGapErrorStandardValue(getCellValue(row19.getCell(7)));
        bo.setMaxAccuracy(getCellValue(sheet.getRow(22).getCell(1)));
        Row row25 = sheet.getRow(25);
        bo.setAccuracyStandardValue(getCellValue(row25.getCell(1)));
        bo.setTestDate(getCellValue(row25.getCell(4)));
        bo.setTestConclusion(getCellValue(row25.getCell(7)));
        iElectronicPendulumDataService.insertByBo(bo);
        for (int i = 29; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row==null){
                break;
            }
            ElectronicPendulumdetailDataBo dbo = new ElectronicPendulumdetailDataBo();
            dbo.setLmtId(bo.getLmtId());
            dbo.setTestDate(bo.getTestDate());
            dbo.setReportNumber(bo.getReportNumber());
            dbo.setDisplacementAndTheoreticalValueL(getCellValue(row.getCell(0)));
            dbo.setDisplacementAndTheoreticalValueU(getCellValue(row.getCell(1)));
            dbo.setMeasuredValuePositive(getCellValue(row.getCell(2)));
            dbo.setMeasuredValueReverse(getCellValue(row.getCell(3)));
            dbo.setLimitErrorPositive(getCellValue(row.getCell(4)));
            dbo.setLimitErrorReverse(getCellValue(row.getCell(5)));
            dbo.setLinearityPositive(getCellValue(row.getCell(6)));
            dbo.setLinearityReverse(getCellValue(row.getCell(7)));
            dbo.setGapError(getCellValue(row.getCell(8)));
            iElectronicPendulumdetailDataService.insertByBo(dbo);
        }
        return R.ok();
    }

    public String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        return cell.getStringCellValue();
    }
}
